package com.trantronghien85.websocketdemo;
import android.content.SharedPreferences;
import android.content.Context;
import android.app.Activity;
class Config {

    public void save(Activity ac) {
        // Storing data into SharedPreferences
        SharedPreferences sharedPreferences = ac.getSharedPreferences("MySharedPref", Context.MODE_PRIVATE);

        // Creating an Editor object to edit(write to the file)
        SharedPreferences.Editor myEdit = sharedPreferences.edit();

// Storing the key and its value as the data fetched from edittext
        myEdit.putString("host", host);
        myEdit.putInt("age", port);

// Once the changes have been made,
// we need to commit to apply those changes made,
// otherwise, it will throw an error
        myEdit.commit();
    }

    public void load(Activity ac) {
        // Retrieving the value using its keys the file name
// must be same in both saving and retrieving the data
// Context.MODE_PRIVATE, Context.MODE_WORLD_READABLE, Context.MODE_WORLD_WRITEABLE, Context.MODE_MULTI_PROCESS
        SharedPreferences sh = ac.getSharedPreferences("MySharedPref", Context.MODE_PRIVATE);

        // The value will be default as empty string because for
// the very first time when the app is opened, there is nothing to show
        host = sh.getString("host", null);
        int a = sh.getInt("age", -1);

// We can then use the data
        port=Integer.valueOf(a);
    }
    public String getUrl(){
        String url=null;
        if(host!=null && port>0)
        {
            url=new String("ws://"+host+":"+port);
        }
        return url;
    }
    static public Config get(){
        if(instance==null){
            instance=new Config();
        }
        return instance;
    }
    private static Config instance;
    public String host;
    public int  port;

}